<?php
class Goodsprofitrate extends CS_Controller
{
    public function _init()
    {
        $this->load->library('pagination');
        $this->load->model('goods_profit_rate_model', 'goods_profit_rate');
    }
    
    public function grid($pg = 1)
    {
        $page_num = 20;
        $num = ($pg-1)*$page_num;
        $config['first_url'] = base_url('goodsprofitrate/grid').$this->pageGetParam($this->input->get());
        $config['suffix'] = $this->pageGetParam($this->input->get());
        $config['base_url'] = base_url('goodsprofitrate/grid');
        $config['total_rows'] = $this->goods_profit_rate->total($this->input->get());
        $config['uri_segment'] = 4;
        $this->pagination->initialize($config);
        $data['pg_link'] = $this->pagination->create_links();
        $data['page_list'] = $this->goods_profit_rate->page_list($page_num, $num, $this->input->get());
        $data['all_rows'] = $config['total_rows'];
        $data['pg_now'] = $pg;
        $data['page_num'] = $page_num;
        $this->load->view('goodsprofitrate/grid', $data);
    }
    
    public function add()
    {
        $this->load->view('goodsprofitrate/add');
    }
    
    public function addPost()
    {
        $d = $this->input->post();
        $_d = $d;
        unset($_d['name']);
        unset($_d['uid']);
        unset($_d['prosecond_id']);
        unset($_d['purchaser_id']);
        unset($_d['purchaser_city_id']);
        if (array_sum($_d) != 100) {
            $this->error('goodsprofitrate/add', '', '比率相加不等于100！');
        }
        $res = $this->goods_profit_rate->insert($d);
        if ($res) {
            $this->success('goodsprofitrate/grid', '', '添加成功！');
        } else {
            $this->error('goodsprofitrate/add', '', '添加失败！');
        }
    }
    
    public function edit($rate_id=0)
    {
        if (!$rate_id || !is_numeric($rate_id) || !is_int($rate_id * 1)) {
            show_404();
        }
        $data['d'] = $this->goods_profit_rate->findByRateId($rate_id)->row_array();
        $this->load->view('goodsprofitrate/edit', $data);
    }
    
    public function editPost()
    {
        $d = $this->input->post();
        if (!isset($d['rate_id']) || !is_numeric($d['rate_id']) || !is_int($d['rate_id'] * 1)) {
            $this->error('goodsprofitrate/grid', '', '非法参数');
        }
        $_d = $d;
        unset($_d['name']);
        unset($_d['uid']);
        unset($_d['rate_id']);
        unset($_d['prosecond_id']);
        unset($_d['purchaser_id']);
        unset($_d['purchaser_city_id']);
        if (array_sum($_d) != 100) {
            $this->error('goodsprofitrate/edit/' . $d['rate_id'], '', '比率相加不等于100！');
        }
        $res = $this->goods_profit_rate->update($d);
        if ($res) {
            $this->success('goodsprofitrate/grid', '', '修改成功！');
        } else {
            $this->error('goodsprofitrate/edit/' . $d['rate_id'], '', '修改失败！');
        }
    }
}